//
//  ForgetPwd.m
//  HB
//
//  Created by 陶秋安 on 16/3/7.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "ForgetPwd.h"
#import "ForgetPwdToModife.h"
#import "ForgetPwdOperation.h"

@implementation ForgetPwd

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.btnNext.enabled=NO;
    //圆角
    self.btnNext.layer.cornerRadius=2.0;
    
    self.btnGetCode.layer.cornerRadius=2.0;
}

-(void) timeTick
{
    timecount++;
    
    [self.btnGetCode setTitle:[NSString stringWithFormat:@"%ld秒后重发",60-timecount] forState:UIControlStateNormal];
    
    if(timecount>=60)
    {
        //关闭定时器
        [timer setFireDate:[NSDate distantFuture]];
        
        self.btnGetCode.enabled=NO;
        
        [self.btnGetCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

-(void) viewWillDisappear:(BOOL)animated
{
    [timer invalidate];
    
    timer = nil;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //显示导航栏
    [super.navigationController setNavigationBarHidden:NO animated:TRUE];
    
}

- (IBAction)btnNextClick:(id)sender
{
    
    if([self.txtCode.text isEqualToString:@""])
    {
        [Common showMsg:@"请输入手机验证码！" andCancel:YES andHost:self];
    }
    else
    {
        NSString *url= [FxGlobal getServiceUrl:@"user" andFuncName:@"VerifyCode"];
        
        NSString *mobile=self.txtMobile.text;
        
        NSString *code1=self.txtCode.text;
        
        NSString *md5Str= [NSString stringWithFormat: @"code=%@&mobile=%@&key=tqa",code1,mobile];
        
        NSString *sign = [Common createMD5:md5Str];
        
        NSString *body=[NSString stringWithFormat:@"{\"mobile\":\"%@\",\"code\":\"%@\",\"sign\":\"%@\",\"clicentid\":\"1\"}",mobile,code1,sign];
        
        NSDictionary *dictInfo = @{@"url":url,@"body":body};
        
        operation = [[ForgetPwdOperation alloc] initWithDelegate:self];
        
        [operation setOpInfo:dictInfo];
        
        [operation post];

    }
 
}

-(void)opSuccessForPost:(id)data
{
    operation= nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"1"])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            //打开下一页
            ForgetPwdToModife *forgetpwdtoModife=[[ForgetPwdToModife alloc]init];
            
            forgetpwdtoModife.userName=userName;
            
            forgetpwdtoModife.djid=djid;
            
            [self.navigationController pushViewController:forgetpwdtoModife animated:true];
        });
    }
    else
    {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [Common showMsg:@"手机验证码错误！" andCancel:YES andHost:self];
        });
    }
}

-(void) next
{
    NSString *mobile= self.txtMobile.text;
    
    operation=[[ForgetPwdOperation alloc] initWithDelegate:self];
    
    NSString *urlS= [FxGlobal getServiceUrl:@"user" andFuncName:@"isexistsmobileforget"];
    
    NSString *url= [NSString stringWithFormat:@"%@?mobile=%@",urlS,mobile];
    
    NSDictionary *dictInfo = @{@"url":url};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
    
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    if (flag == 0)
    {
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSDictionary *data1 = [dataStr objectFromJSONString];
        
        NSString *v=[data1 objectForKey:@"result"];
        
        if([v isEqualToString:@"1"])
        {
            userName=[data1 objectForKey:@"nickname"];
            
            djid=[data1 objectForKey:@"djID"];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                timecount=0;
                
                if(timer == nil)
                {
                    timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeTick) userInfo:nil repeats:YES];
                }
                else{
                    //开启定时器
                    [timer setFireDate:[NSDate distantPast]];
                }
                
                self.btnGetCode.enabled=NO;
                
                self.btnNext.enabled=YES;
                
                [Common showMsg:@"手机验证码已经发送！" andCancel:YES andHost:self];
            });
        }
        else
        {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [Common showMsg:@"手机号不存在！" andCancel:YES andHost:self];
            });
            
        }
    }
}

- (IBAction)btnGetCodeClick:(id)sender
{
    
    NSString *mobile= self.txtMobile.text;
    
    if(![Common isMobileNumber:mobile])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [Common showMsg:@"请输入正确的手机号！" andCancel:YES andHost:self];
        });
        
    }
    else
    {
        [self next];
    }
}
@end
